﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Ext.Net;
using System.Configuration;

namespace LongSpeed.WebSite.Behaviors
{
    public class Session : Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (CurrentLoginUser == null)
            {
                string url = string.Format("{0}/Login.aspx?reUrl={1}", GetRootUrl(this), Request.Url.AbsoluteUri);
                base.Response.Redirect(url);
            }
            base.OnInit(e);
        }

        User _CurrentLoginUser;
        public User CurrentLoginUser
        {
            get
            {
                if (_CurrentLoginUser == null)
                {
                    _CurrentLoginUser = Session[SessionString.CurrentUser] as User;
                }
                return _CurrentLoginUser;
            }
        }
        public static string GetRootUrl(Page currentPage)
        {
            string page = currentPage.AppRelativeVirtualPath.TrimStart('~');
            int index = currentPage.Request.Url.AbsoluteUri.IndexOf(page);
            if (index >= 0)
            {
                return currentPage.Request.Url.AbsoluteUri.Substring(0, index);
            }
            else
            {
                throw new UriFormatException("URI格式错误 - " + currentPage.Request.Url.AbsoluteUri);
            }
        }

        public static string Login(string userName, string pwd, string verifcationCode, Page currentPage)
        {
            var verificationCode = currentPage.Session[SessionString.VerificationCode];
            if (verificationCode != null && verifcationCode == verificationCode.ToString())
            {
                return Login(userName, pwd, currentPage);
            }
            else if (verificationCode != null)
            {
                return "您输入的验证码不正确,请重新输入...";
            }
            else
            {
                var msgbtnConfig = new MessageBoxButtonsConfig()
                {
                    Ok = new MessageBoxButtonConfig()
                    {
                        Handler = string.Format("window.location.href='{0}';", currentPage.Request.Url.ToString()),
                        Text = "确定",
                    }
                };
                string title = ConfigurationManager.ConnectionStrings["MsgAlert"].ConnectionString;
                string content = "验证码过期或者加载不成功...<br />点击<span style=\"color:#f00;\">确定</span>刷新本页面...";

                return content;
            }
        }
        public static string Login(string userName, string pwd, Page currentPage)
        {
            var users = new User().GetUsersByUserIdentity(UserIdentity.管理员);
            var u = users.Where(o => o.LoginName == userName && o.Pwd == pwd).SingleOrDefault();
            if (u != null)
            {
                HttpContext.Current.Session[SessionString.CurrentUser] = u;
                string reUrl = currentPage.Request.QueryString["reUrl"];
                string url = string.Empty;
                if (string.IsNullOrEmpty(reUrl))
                {
                    url = string.Format("{0}/Main/Admin/AdminDeskTop.aspx", GetRootUrl(currentPage));
                }
                else
                {
                    url = reUrl;
                }
                Ext.Net.X.Redirect(url,"Loading……");
                //currentPage.Response.Redirect(url);
                return "true";
            }
            else
            {
                return "对不起,您输入的用户名或密码不正确,请重新输入...";
            }
        }
        public static void LoginOut()
        {
            HttpContext.Current.Session[SessionString.CurrentUser] = null;
            Ext.Net.X.Redirect("~/Login.aspx","Login Out……");
        }
    }
}